from django.shortcuts import render,redirect,reverse
from .models import Article
from django.http import HttpResponse
from django.views.decorators.http import require_http_methods,require_GET,require_POST,require_safe
# Create your views here.
# @require_http_methods(['GET'])
@require_GET
def index(request):
    articles = Article.objects.all()
    return render(request,'index.html',context={"articles":articles})
# @require_http_methods(['GET','POST'])
# def add_article(request):
#     if request.method == 'GET':
#         return render(request,'add_article.html')
#     else:
#         name = request.POST.get('title')
#         content = request.POST.get('content')
#         result = Article.objects.filter(title=name).first()
#         if not result:
#             Article.objects.create(title=name,content=content)
#             return redirect(reverse('index'))
#         else:
#             return render(request,'add_article.html',context={"errors":"用户名已经存在"})

def add_article(request):
    articles = []
    for x in range(150):
        # article = Article.objects.bulk_create([
        #     Article(title='标题:%s'%x,content="内容%s"%x)
        #
        # ])
        article = Article(title='标题:%s'%x,content="内容%s"%x)
        articles.append(article)
    Article.objects.bulk_create(articles)
    return HttpResponse("ok")
